package com.yu.work.fragment;

import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.support.annotation.Nullable;
import android.support.design.widget.Snackbar;
import android.support.v4.app.Fragment;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.yu.work.R;
import com.yu.work.RecordEditActivity;
import com.yu.work.adapter.RecordListAdapter;
import com.yu.work.domain.Record;
import com.yu.work.service.RecordService;

import org.xutils.view.annotation.ContentView;
import org.xutils.view.annotation.ViewInject;
import org.xutils.x;

import java.util.List;

/**
 * 保存的记录列表页面
 */
@ContentView(R.layout.fragment_record_list)
public class RecordListFragment extends Fragment {

    private static final int REFRESH = 1;

    @ViewInject(R.id.recycler_save_list)
    private RecyclerView mRecyclerView;

    private RecordListAdapter mAdapter;
    private List<Record> mRecords;
    private RecordService mRecordService = RecordService.getInstance();
    private RefreshThread refreshThread;

    private Handler mHandler = new Handler() {
        @Override
        public void handleMessage(Message msg) {
            switch (msg.what) {
                case REFRESH:
                    updateUI();
                    break;
            }
        }
    };

    /**
     * 更新UI
     */
    private void updateUI() {
        mRecords = mRecordService.getAll();
        mAdapter.setRecords(mRecords);
    }

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container,
                             Bundle savedInstanceState) {
        View v = x.view().inject(this, inflater, container);
        initRecyclerView();
        refreshThread = new RefreshThread();
        refreshThread.start();
        return v;
    }

    /**
     * 初始化RecyclerView
     * 包括创建Adapter和设置的点击事件
     */
    private void initRecyclerView() {
        mAdapter = new RecordListAdapter(getActivity(), mRecords);
        mAdapter.setOnItemClickListener(new RecordListAdapter.OnItemClickListener() {
            @Override
            public void onItemClick(Record record) {
                Intent intent = RecordEditActivity.newIntent(getActivity(), record);
                getActivity().startActivity(intent);
            }
        });
        mAdapter.setOnItemLongClickListener(new RecordListAdapter.OnItemLongClickListener() {
            @Override
            public void onItemLongClick(final Record record) {
                Snackbar.make(mRecyclerView, "是否删除", Snackbar.LENGTH_LONG)
                        .setAction("确定", new View.OnClickListener() {
                            @Override
                            public void onClick(View v) {
                                Long id = record.getId();
                                mRecordService.delete(id);
                                updateUI();
                            }
                        })
                        .show();
            }
        });

        mRecyclerView.setLayoutManager(new LinearLayoutManager(getActivity()));
        mRecyclerView.setAdapter(mAdapter);
    }

    /**
     * 刷新UI线程
     */
    class RefreshThread extends Thread {

        private boolean flag = true;

        public void setFlag(boolean flag) {
            this.flag = flag;
        }

        @Override
        public void run() {
            while (flag) {
                Message msg = new Message();
                msg.what = REFRESH;
                mHandler.sendMessage(msg);

                try {
                    Thread.sleep(1000);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }
        }
    }

}
